%Goal: mortgage market equilibrium




function [rno, sno, rnbl,snbl,rnb,snb,rm,sbm] = Solve_profit_mbank(psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo,rhob,rhobl,sow,sbw,rb,rbl)

MaxIterations=10000;
tol = 10^(-9);
rm_initial = 1.81;
rm = [];
diff = 1000;
iter = 0;

while diff>tol && iter<=MaxIterations  
        iter = iter+1;
        [rno, sno, rnbl,snbl,rnb,snb] = Solve_profit_local_other(psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo,rhob,rhobl,sow,sbw,rb,rbl,rm_initial);
        sbm = exp(-alpha*rm_initial+qn)/(1+exp(-alpha*rbl+qb)+(Nb-1)*exp(-alpha*rb+qb)+Nm*exp(-alpha*rm_initial+qb)+Nn*sow*exp(-alpha*rno+qn)+Nn*sbw*exp(-alpha*rnbl+qn)+Nn*sbw*(Nb-1)*exp(-alpha*rnb+qn));
        rm = psim+1/(alpha*(1-sbm));
        diff = abs(rm-rm_initial);
        rm_initial = rm;
  
        
  
end 
end





